// key-value 模型
public class HashBucket {
    private static class Node {
        private int key;
        private int value;
        Node next;
        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }
 
 
    private Node[]  array;
    private int size;   // 当前的数据个数
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 8;//默认桶的大小
 
    public void put(int key, int value) {
        Node node=new Node(key,value);
        //扩容
        int index=key%array.length;
        Node cur=array[index];
        while(cur!=null){
            if(cur.key==key){
                cur.value=value;
                return;
            }
            cur=cur.next;
        }
        node.next=array[index];
        array[index]=node;
        size++;
        if(loadFactor()>=LOAD_FACTOR){
            resize();
        }
    }
 
 
    private void resize() {
        Node[] newArray = new Node[2*array.length];
        //拷贝
        for(Node node:array){
            Node cur=node;
            while(cur!=null){
                int index=cur.key%newArray.length;
                Node curnext=cur.next;
                cur.next=newArray[index];
                newArray[index]=cur;
                cur=curnext;
            }
        }
        this.array = newArray;
    }
 
 
    private double loadFactor() {
        return size * 1.0 / array.length;
    }
 
 
    public HashBucket() {
        this.array=new Node[DEFAULT_SIZE];
    }
 
 
    public int get(int key) {
         int index=key%array.length;
         Node cur=array[index];
         while(cur!=null){
             if(cur.key==key){
                 return cur.value;
             }
             cur=cur.next;
         }
         return -1;
    }
}